package beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class StudentDao {
	static String DB_URL = "jdbc:mysql://localhost:8889/mydb?useUnicode=true&characterEncoding=utf-8";
	static String DB_USER = "root";
	static String DB_PASSWORD = "root";
	
	
	public static ArrayList getStudentList() throws ClassNotFoundException {
		
		ArrayList students = new ArrayList();
		try {
//			Connection conn = null;
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
			PreparedStatement smt = conn.prepareStatement("SELECT * FROM student");
			ResultSet rs = smt.executeQuery();
			System.out.println(rs);
			while(rs.next()){
				
				int id = rs.getInt("id");
				String name = rs.getString("name");
				boolean gender = rs.getBoolean("gender");
				
				Student student = new Student();
				
				student.setId(id);
				student.setName(name);
				student.setGender(gender);
				
				students.add(student);
			}
			
			smt.close();
			rs.close();
			conn.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return students;
		
	}
}
